package com.cxhome.cxinfox.routing;

import java.util.Date;

import org.apache.log4j.Logger;

import com.jfinal.plugin.activerecord.Page;
import com.traffic.managent.base.BaseService;
import com.traffic.managent.base.Paginator;
import com.traffic.managent.util.SQLUtils;
import com.traffic.managent.util.UtilsUtils;

/**
 * 现代路由业务类
 * 
 */
public class ModernRoutingService extends BaseService {

	private final static Logger log = Logger.getLogger(ModernRoutingService.class);
	public static final ModernRoutingService service = new ModernRoutingService();

	public Page<ModernRouting> paginate(ModernRouting model, Paginator paginate, StringBuilder orderSql) {

		SQLUtils sql = new SQLUtils(
				"from ( SELECT  cmr1.*,  cp1.providerName providerName, cpv1.vasProdName vasPidName,"
						+ "crp1.province as provinceName, cph1.cityName  , cci1.customerName as customerName "
						+ "from cx_modern_routing cmr1  "
						+ "LEFT JOIN cx_customer_info cci1 on cmr1.customerId=cci1.customerId "
						+ "LEFT JOIN cx_provider cp1 ON cmr1.provider =cp1.ids  "
						+ "LEFT JOIN cx_product_vas cpv1 ON cmr1.vasPid = cpv1.ids  LEFT JOIN "
						+ "cx_r_province crp1 ON crp1.headId = cmr1.province LEFT JOIN "
						+ "(SELECT DISTINCT areaCode,cityName,province FROM cx_phone_head) cph1 ON cph1.areaCode = cmr1.numberAttribution ) cxinfo "
						+ "where 1=1 ");

		if (model.getAttrValues().length != 0) {
			sql.whereEquals("status", model.getStr("status"));
			sql.whereEquals("provider", model.getStr("provider"));
			sql.whereEquals("customerId", model.getStr("customerId"));
		}
		sql.append(orderSql);
		return ModernRouting.dao.paginate(paginate, " select cxinfo.*  ", sql.toString().toString());

	}
	
	
	/**
	 * 查看详情
	 */
	public ModernRouting view(String ids) {
		String sql = "select cxinfo.* from ( "
				+ "SELECT cmr1.*, cp1.providerName providerName,cpv1.vasProdName vasPidName,crp1.province as provinceName,cph1.cityName  ,cci1.customerName as customerName "
				+ "from cx_modern_routing cmr1 " + "LEFT JOIN cx_customer_info cci1 on cmr1.customerId=cci1.customerId "
				+ "LEFT JOIN cx_provider cp1 ON cmr1.provider =cp1.ids "
				+ "LEFT JOIN cx_product_vas cpv1 ON cmr1.vasPid = cpv1.ids "
				+ "LEFT JOIN cx_r_province crp1 ON crp1.headId = cmr1.province "
				+ "LEFT JOIN (SELECT DISTINCT areaCode,cityName,province FROM cx_phone_head) cph1 ON cph1.areaCode = cmr1.numberAttribution "
				+ ") cxinfo where 1=1 and cxinfo.ids = ? ";
		return ModernRouting.dao.findFirst(sql, ids);
	}
	
	/**
	 * 新增或者更新功能
	 * @param model
	 * @param productvasIds
	 * @param user
	 */
	public void saveOrUpdate(ModernRouting model, String productvasIds, String user) {
		// 新增
		String[] proidsDiscountstr = productvasIds.split(",");
		for (int i = 0; i < proidsDiscountstr.length; i++) {
			// 每一个产品+折扣
			String vasPid = proidsDiscountstr[i];

			model.put("vasPid", vasPid);
			model.put("createPerson", user);
			model.put("createTime", new Date());
			model.put("isExecutor", "0");
			model.put("level", "1");
			model.put("ids", UtilsUtils.getRandomId(30));
			model.save();

		}
	}
	
	/**
	 * 删除
	 * @param ids
	 * @param user
	 */
	public void delete(String ids, String user) {
		ModernRouting.dao.deleteByidstoStatus(ids, user);

	}


}
